package com.neiru.divider.activity;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageButton;
import android.widget.Toast;

import com.neiru.divider.R;
import com.neiru.divider.bean.Level;
import com.neiru.divider.bean.Player;
import com.neiru.divider.constants.ActivityConstants;
import com.neiru.divider.facade.LevelFacade;
import com.neiru.divider.facade.PlayerFacade;

public class Levels extends Activity {

	private Level level;
	private Player player;
	private LevelFacade levelFacade;
	private PlayerFacade playerFacade = null;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		if(savedInstanceState != null) {
			this.level = (Level)savedInstanceState.getSerializable(ActivityConstants.PLAY_LEVEL_DATA_NAME);
			this.player = (Player) savedInstanceState.getSerializable(ActivityConstants.PLAY_LEVEL_DATA_PLAYER);
		}
		if(this.player == null && getIntent().getExtras() != null) {
			this.player = (Player)getIntent().getExtras().getSerializable(ActivityConstants.PLAY_LEVEL_DATA_PLAYER);
		}
		if(this.level == null && getIntent().getExtras() != null) {
			this.level = (Level)getIntent().getExtras().getSerializable(ActivityConstants.PLAY_LEVEL_DATA_NAME);
		}
		
		if(this.level == null) {
			this.level = new Level(1, new ArrayList<Integer>(), 3, 1, 3, true);
		}
		
		requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
                                WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.levels);
		inizialiceValues();
		configureButtons();
	}
	private void inizialiceValues() {
		this.levelFacade = new LevelFacade(this);
		this.playerFacade = new PlayerFacade(this);
	}
	
	/**
	 * Se configuran todos los botones manualmente, desde el 
	 */
	private void configureButtons() {
		switch(this.level.getLevel()) {
		case 35:
			configureLevelFromId(R.id.level_35,35,R.drawable.level_35_unlock);
		case 34:
			configureLevelFromId(R.id.level_34,34,R.drawable.level_34_unlock);
		case 33:
			configureLevelFromId(R.id.level_33,33,R.drawable.level_33_unlock);
		case 32:
			configureLevelFromId(R.id.level_32,32,R.drawable.level_32_unlock);
		case 31:
			configureLevelFromId(R.id.level_31,31,R.drawable.level_31_unlock);
		case 30:
			configureLevelFromId(R.id.level_30,30,R.drawable.level_30_unlock);
		case 29:
			configureLevelFromId(R.id.level_29,29,R.drawable.level_29_unlock);
		case 28:
			configureLevelFromId(R.id.level_28,28,R.drawable.level_28_unlock);
		case 27:
			configureLevelFromId(R.id.level_27,27,R.drawable.level_27_unlock);
		case 26:
			configureLevelFromId(R.id.level_26,26,R.drawable.level_26_unlock);
		case 25:
			configureLevelFromId(R.id.level_25,25,R.drawable.level_25_unlock);
		case 24:
			configureLevelFromId(R.id.level_24,24,R.drawable.level_24_unlock);
		case 23:
			configureLevelFromId(R.id.level_23,23,R.drawable.level_23_unlock);
		case 22:
			configureLevelFromId(R.id.level_22,22,R.drawable.level_22_unlock);
		case 21:
		case 20:
			configureLevelFromId(R.id.level_21,21,R.drawable.level_21_unlock);
			configureLevelFromId(R.id.level_20,20,R.drawable.level_20_unlock);
		case 19:
			configureLevelFromId(R.id.level_19,19,R.drawable.level_19_unlock);
		case 18:
			configureLevelFromId(R.id.level_18,18,R.drawable.level_18_unlock);
		case 17:
			configureLevelFromId(R.id.level_17,17,R.drawable.level_17_unlock);
		case 16:
			configureLevelFromId(R.id.level_16,16,R.drawable.level_16_unlock);
		case 15:
			configureLevelFromId(R.id.level_15,15,R.drawable.level_15_unlock);
		case 14:
			configureLevelFromId(R.id.level_14,14,R.drawable.level_14_unlock);
		case 13:
			configureLevelFromId(R.id.level_13,13,R.drawable.level_13_unlock);
		case 12:
			configureLevelFromId(R.id.level_12,12,R.drawable.level_12_unlock);
		case 11:
			configureLevelFromId(R.id.level_11,11,R.drawable.level_11_unlock);
		case 10:
			configureLevelFromId(R.id.level_10,10,R.drawable.level_10_unlock);
		case 9:
			configureLevelFromId(R.id.level_9,9,R.drawable.level_9_unlock);
		case 8:
			configureLevelFromId(R.id.level_8,8,R.drawable.level_8_unlock);
		case 7:
			configureLevelFromId(R.id.level_7,7,R.drawable.level_7_unlock);
		case 6:
			configureLevelFromId(R.id.level_6,6,R.drawable.level_6_unlock);
		case 5:
			configureLevelFromId(R.id.level_5,5,R.drawable.level_5_unlock);
		case 4:
			configureLevelFromId(R.id.level_4,4,R.drawable.level_4_unlock);
		case 3:
			configureLevelFromId(R.id.level_3,3,R.drawable.level_3_unlock);
		case 2:
			configureLevelFromId(R.id.level_2,2,R.drawable.level_2_unlock);
		case 1: 
		default:
			configureLevelFromId(R.id.level_1,1,R.drawable.level_1_unlock);
			break;
		}
	}
	private void configureLevelFromId(int id, Integer levelNumber, int unlockBackgroundId) {
		ImageButton levelButton  = (ImageButton) findViewById(id);
		levelButton.setBackgroundDrawable(getResources().getDrawable(unlockBackgroundId));
		final Integer number = levelNumber;
		levelButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Level level = Levels.this.levelFacade.getLevel(number);
				Intent i = new Intent(Levels.this, Play.class);
				i.putExtra(ActivityConstants.PLAY_LEVEL_DATA_NAME, level);
				startActivityForResult(i, ActivityConstants.ACTIVITY_PLAY);
			}
		});
	}
	
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		Log.d("Levels",requestCode+"=="+ActivityConstants.ACTIVITY_PLAY+"??");
		switch(requestCode) {
			case ActivityConstants.ACTIVITY_PLAY:
				if(resultCode == ActivityConstants.PLAY_RESULT_NEXT_LEVEL) {
					Level passedLevel = (Level)data.getExtras().getSerializable(ActivityConstants.PLAY_LEVEL_DATA_NAME);
					Long score = data.getExtras().getLong(ActivityConstants.PLAY_LEVEL_DATA_SCORE);
					if(score != null) {
						player.setScore( ((Long)(player.getScore()+score)).intValue());
					}

					if(passedLevel.getLevel() >= this.levelFacade.getLastLevel()) {
						//Si se ha pasado el juego, sacar la pantalla de congratulations you are very coool
						Intent i = new Intent(Levels.this, GameComplete.class);
						startActivityForResult(i, ActivityConstants.ACTIVITY_GAME_COMPLETE);
						return;
					}else if(passedLevel.getLevel() >= player.getLastLevel()) {
						player.setLastLevel(passedLevel.getLevel() + 1);
					}
					playerFacade.updatePlayer(player);
					//Iniciar la siguiente pantalla
					this.level = Levels.this.levelFacade.getLevel(passedLevel.getLevel()+1);
					Intent i = new Intent(Levels.this, Play.class);
					i.putExtra(ActivityConstants.PLAY_LEVEL_DATA_NAME, this.level);
					startActivityForResult(i, ActivityConstants.ACTIVITY_PLAY);
				} else if(resultCode == ActivityConstants.PLAY_RESULT_FINISH) {
					Level passedLevel = (Level)data.getExtras().getSerializable(ActivityConstants.PLAY_LEVEL_DATA_NAME);
					//TODO Cambiar esto, para que guarde el Score
					if(passedLevel.getLevel() >= player.getLastLevel()) 	{
						player.setLastLevel(passedLevel.getLevel() + 1);
					}
					Long score = data.getExtras().getLong(ActivityConstants.PLAY_LEVEL_DATA_SCORE);
					if(score != null){
						player.setScore( ((Long)(player.getScore()+score)).intValue());
					}
					this.level = Levels.this.levelFacade.getLevel(passedLevel.getLevel()+1);
					playerFacade.updatePlayer(player);
					
				} else {
					showGameOverMessage();
				}
				break;
		}
		configureButtons();
	}
	private void showGameOverMessage() {
		Toast.makeText(this, "GAME OVER", Toast.LENGTH_LONG).show();
	}
	protected void onResume() {
		super.onResume();
		if(this.playerFacade != null) {
			this.player = this.playerFacade.getPlayer();
		}
	}

}
